﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Security.Cryptography;
using System.IO;

namespace LibraryUtility.Common
{
    /// <summary>
    /// ClassName: Computer Infomation.
    /// Auther:Leo.wl
    /// Time:2010-04-15
    /// </summary>
    public  class ComputerInfo
    {
        /// <summary>
        /// Gets the cpu id.
        /// </summary>
        /// <returns></returns>
        public string GetCpuId()
        {
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();

            String strCpuID = null;
            foreach (ManagementObject mo in moc)
            {
                strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                break;
            }
            return strCpuID;
        }

        /// <summary>
        /// Gets the name of the host.
        /// </summary>
        /// <returns></returns>
        public string GetHostName()
        {
            return System.Net.Dns.GetHostName();
        }

        /// <summary>
        /// Gets the main hard disk id.
        /// </summary>
        /// <returns></returns>
        public string GetMainHardDiskId()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            String strHardDiskID = null;
            foreach (ManagementObject mo in searcher.Get())
            {
                strHardDiskID = mo["SerialNumber"].ToString().Trim();
                break;
            }
            return strHardDiskID;
        }

    }
    /// <summary>
    /// ClassName:My DES
    /// Auther:Leo.wl
    /// Time:2010-04-15
    /// </summary>
    [DebuggerStepThrough()]
    public class MyDES
    {
        private string _Key;
        public MyDES(string key)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentException("des key is error!");

            _Key = ConvertKey(key);
        }
        private string ConvertKey(string key)
        {
            if (key.Length > 8)
                return key.Substring(0, 8);
            else if (key.Length < 8)
                return key.PadRight(8, key[0]);
            else
                return key;
        }
        public string Key
        {
            get
            {
                return _Key;
            }
        }
        #region
        /// <summary>
        /// 加码
        /// </summary>
        /// <param name="pToEncrypt"></param>
        /// <returns></returns>
        public string Encrypt(string pToEncrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(_Key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(_Key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
                ret.AppendFormat("{0:X2}", b);
            return ret.ToString();
        }

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="pToDecrypt"></param>
        /// <returns></returns>
        public string Decrypt(string pToDecrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(_Key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(_Key);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                }
                StringBuilder ret = new StringBuilder();
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
        }
        #endregion
    }
}
